package com.xkcyy.one.config;

import com.example.demo.util.DatabaseInitializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.ApplicationArguments;
import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * SQLite数据库配置类
 *
 * @author AI Assistant
 */
@Configuration
public class SQLiteConfig implements CommandLineRunner {
    private static final String DB_PATH = "data/one-agent.db";
    private static final String INIT_SCRIPT = "src/main/resources/scripts/init.sql";
    private static final String INIT_DATA_SCRIPT = "src/main/resources/scripts/init_data.sql";

    public static void initializeDatabase() {






        File dbFile = new File(DB_PATH);
        if (!dbFile.exists()) {
            try {
                // 创建data目录
                Files.createDirectories(Paths.get("data"));

                // 连接SQLite数据库
                Connection connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
                Statement statement = connection.createStatement();

                // 执行初始化脚本
                String initSql = new String(Files.readAllBytes(Paths.get(INIT_SCRIPT)));
                statement.executeUpdate(initSql);

                // 执行初始化数据脚本
                String initDataSql = new String(Files.readAllBytes(Paths.get(INIT_DATA_SCRIPT)));
                statement.executeUpdate(initDataSql);

                statement.close();
                connection.close();

                System.out.println("数据库初始化完成");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run(String... args) throws Exception {
        initializeDatabase();
    }

//    /**
//     * 配置数据库初始化
//     *
//     * @param dataSource 数据源
//     * @return DataSourceInitializer
//     */
//    @Bean
//    public DataSourceInitializer dataSourceInitializer(@Autowired DataSource dataSource) throws IOException {
//        DataSourceInitializer initializer = new DataSourceInitializer();
//        initializer.setDataSource(dataSource);
//        File dbFile = new File(DB_PATH);
//        if (dbFile.exists()) {
//            return initializer;
//        }
//        Files.createDirectories(Paths.get("data"));
//
//        // 设置数据库schema
//        ResourceDatabasePopulator schemaPopulator = new ResourceDatabasePopulator();
//        schemaPopulator.addScript(new ClassPathResource("scripts/init.sql"));
//        initializer.setDatabasePopulator(schemaPopulator);
//
//        // 设置数据库初始数据
//        ResourceDatabasePopulator dataPopulator = new ResourceDatabasePopulator();
//        dataPopulator.addScript(new ClassPathResource("scripts/init_data.sql"));
//        initializer.setDatabaseCleaner(dataPopulator);
//
//        return initializer;
//    }
}
